<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <div id="app">
        <!-- 某些结果是基于之前数据实时计算出来的，我们可以利用计算属性来完成 -->
        <ul>
            <li>西游记；价格：{{xyjPrice}}，数量：<input type="number" v-model="xyjNum"></li>
            <li>西游记；价格：{{shzPrice}}，数量：<input type="number" v-model="shzNum"></li>
            <li>总价：{{totalPrice}}</li>
            {{msg}}
        </ul>

    </div>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                xyjPrice: 99.98,
                shzPrice: 98,
                xyjNum: 1,
                shzNum: 1,
                msg: ''

            },
            //计算属性
            computed: {
                totalPrice() {
                    return this.xyjPrice * this.xyjNum + this.shzPrice * this.shzNum;
                }
            },
            //监听器
            watch: {
                xyjNum(newVal, oldVal) {
                    if (newVal >= 3) {
                        console.log('库存超出限制')
                        this.msg = '库存超出限制';
                        this.xyjNum = 3;
                    } else {
                        console.log('else')
                        this.msg = ''
                    }
                }
            }
        });
    </script>
</body>

</html>